package com.bsh.erp.vto.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bsh.erp.base.BaseDaoImpl;
import com.bsh.erp.entity.Department;
import com.bsh.erp.entity.Staffer;
import com.bsh.erp.service.DepartmentService;
import com.bsh.erp.service.StafferService;
import com.bsh.erp.vto.bean.StafferVTO;
import com.bsh.erp.vto.service.StafferVTOService;

/**
 * StafferVTO 数据库操作接口的实现
 * @author JiaQi
 *
 */
@Service
@Transactional
public class StafferVTOServiceImpl extends BaseDaoImpl<StafferVTO> implements StafferVTOService{

	/**
	 * 获取全部 StafferVTO
	 * @author JiaQi
	 * @return 全部StafferVTO
	 */
	public List<StafferVTO> findAllStafferVTO() {
		List<StafferVTO> stafferVTOs = new ArrayList<StafferVTO>();
//		获取全部Staffer
		List<Staffer> staffers = stafferService.findAll();
//		遍历赋值
		for( int i = 0 ; i < staffers.size() ; i++) {
			StafferVTO stafferVTO = new StafferVTO();
			Staffer superior = stafferService.getById(staffers.get(i).getSuperiorId());
			Department department = departmentService.getById(staffers.get(i).getDepartmentId());
			stafferVTO.setDepartment(department);
			stafferVTO.setStaffer(staffers.get(i));
			stafferVTO.setSuperior(superior);
			
			stafferVTOs.add(stafferVTO);
		}
		return stafferVTOs;
	}
	
	/**
	 * 通过传入的List<Staffer> 获取List.VTO
	 * @author JiaQi
	 * @param  传入需要添加上级和部门的list
	 * @return 获取的添加部门和上级List.VTO
	 */
	public List<StafferVTO> findStafferVTOByStaffer(List<Staffer> staffers) {
		List<StafferVTO> stafferVTOs = new ArrayList<StafferVTO>();
//		遍历赋值
		for( int i = 0 ; i < staffers.size() ; i++) {
			StafferVTO stafferVTO = new StafferVTO();
			Staffer superior = stafferService.getById(staffers.get(i).getSuperiorId());
			Department department = departmentService.getById(staffers.get(i).getDepartmentId());
			stafferVTO.setDepartment(department);
			stafferVTO.setStaffer(staffers.get(i));
			stafferVTO.setSuperior(superior);
			
			stafferVTOs.add(stafferVTO);
		}
		return stafferVTOs;
	}
	
//	注入service
	@Autowired
	private StafferService stafferService;
	@Autowired
	private DepartmentService departmentService;
}
